Skip to main content

UiNode

Represents a flexible, composable UI element that manages its own layout, style, and interaction. Each UiNode can contain children, handle user events, and be styled dynamically.

Constructor

new UiNode(style = {}, props = {})

Parameters

NameTypeDescription
stylestructFlexPanel style properties (position, width, height, etc.).
propsstructBehavior and configuration options (e.g., pointerEvents, visible, onDraw).

Core properties

PropertyTypeDescription
idintegerUnique node identifier.
typestringAlways "UiNode".
isUiNodebooleanTrue for all UI nodes.
parentUiNodeundefined
childrenarray<UiNode>List of child nodes.
childrenLengthintegerCached child count.
visiblebooleanWhether the node is visible.
displaybooleanFlexPanel display flag.
pointerEventsbooleanWhether the node reacts to pointer events.
borderbooleanIf true, draws a border for debugging.
borderColorintColor of the border if enabled.
draggablebooleanEnables drag behavior.
dropzonebooleanEnables node to receive dropped elements.
scrollToprealCurrent scroll offset (for scrollable parents).
layoutstructLayout box (position, margins, paddings, size).
onDrawfunctionCalled every frame before drawing.
onDestroyfunctionCalled when the node is destroyed.

Hierarchy methods

MethodDescription
add(...nodes)Adds one or more children to this node.
remove(node)Removes a child from this node.
clear()Removes all children (but does not destroy them).
destroy()Deletes this node and its children from memory.
destroyChildren()Deletes all children but keeps this node alive.
count()Returns the number of direct children.
countAll()Returns the number of nodes in the subtree (recursive).

Traversal

MethodDescription
traverse(cb, recursive = true)Calls cb(node) on self and all descendants.
traverseChildren(cb, recursive = true)Calls cb(node) on children (recursively if true).
reduceChildren(cb, acc, recursive = true)Reduces the node tree into a single value.

Layout & style

MethodDescription
setSize(w, h)Sets node width and height.
setWidth(value) / getWidth()Sets or gets the node width.
setHeight(value) / getHeight()Sets or gets the node height.
setLeft(value) / getLeft()Sets or gets the left position.
setTop(value) / getTop()Sets or gets the top position.
setRight(value) / getRight()Sets or gets the right position.
setBottom(value) / getBottom()Sets or gets the bottom position.

Margin helpers

MethodDescription
setMarginTop(value) / getMarginTop()Top margin.
setMarginLeft(value) / getMarginLeft()Left margin.
setMarginRight(value) / getMarginRight()Right margin.
setMarginBottom(value) / getMarginBottom()Bottom margin.

Visibility

MethodDescription
show()Displays the node.
hide()Hides the node.
isVisible()Returns true if visible and inside scroll bounds.

Event System

The UiNode class supports DOM-like event bubbling and capture phases, including mouse and wheel interactions. Events propagate along the parent chain unless stopped.

Available Events

UI_EVENT.wheelup
UI_EVENT.wheeldown
UI_EVENT.mousedown
UI_EVENT.mouseup
UI_EVENT.click
UI_EVENT.mouseover
UI_EVENT.mouseout
UI_EVENT.mouseenter
UI_EVENT.mouseleave

Event methods

MethodDescription
addEventListener(event, callback, useCapture = false)Adds an event listener (capture or bubble).
removeEventListener(event, callback, useCapture = false)Removes an event listener.
clearEventListeners(event)Clears all listeners of a specific event.
dispatchEvent(event, target)Dispatches an event manually (bubbles/captures).

Shorthand listeners

MethodDescription
onClick(cb)Triggered when clicked.
onMouseDown(cb)Triggered on mouse down.
onMouseUp(cb)Triggered on mouse up.
onMouseEnter(cb)Triggered when the mouse enters.
onMouseLeave(cb)Triggered when the mouse leaves.
onWheelUp(cb)Triggered on scroll up.
onWheelDown(cb)Triggered on scroll down.
click()Manually triggers a click event.
onStep(cb)Adds a per-frame step handler.

🧠 Note: The UI system uses an internal spatial partitioning grid for event dispatch, optimizing hit detection and interaction on complex interfaces.

Drag & Drop

PropertyDescription
draggableEnables dragging.
dropzoneAllows receiving dropped elements.
CallbackDescription
onDragStartCalled when dragging starts.
onDragCalled while dragging.
onDragEndCalled when dragging stops.
onDropCalled when another node is dropped over.

Scrollbars

MethodDescription
enableScrollbar(thumbColor?)Adds a vertical scrollbar to the node.
disableScrollbar()Removes the scrollbar.

Naming

MethodDescription
setName(name)Sets the internal FlexPanel node name.
getName()Returns the node name.

⚡ Performance Notes

  • UiNode is backed by GameMaker's FlexPanel functions, a lightweight Yoga-style layout engine.
  • The event system uses a spatial partitioning grid to efficiently resolve pointer targets.
  • Internal caching (e.g., for scroll bounds) avoids redundant computations.
  • All core methods are marked with gml_pragma("forceinline") for maximum performance.